package com.tianqu.tms.www.attn.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.AttnUpdateAccessCardInfoBo;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralcardUpdateHistory;
import com.tianqu.tms.www.attn.viewhelper.AttnIntegralViewHelper;

/**
 * @author zhangxueliang
 *经办人积分信息修改结果controller
 *
 */
public class AttnIntegralUpdateResultController extends TmsController{
	private Logger logger = Logger.getLogger(AttnIntegralUpdateResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Attn attn = Attn.getInstance();
			AttnIntegralViewHelper viewhelper = (AttnIntegralViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			attn.setAccessCardId(viewhelper.getAccessCardIdNew());
			attn.setAccessCardNo(viewhelper.getAccessCardNoNew());
			attn.setAttnId(viewhelper.getAttnId());
			TmsUserProfile profile = uc.getUserProfile();
			attn.setUpdateUserId(profile.getUniqueId());
			attn.updateIntegralcard(attn);
			
			IntegralcardUpdateHistory history = IntegralcardUpdateHistory.getInstance();
			history = ToHistory(history,viewhelper);
			history.setInsertUserId(profile.getUniqueId());
			history.setMemberId(viewhelper.getAttnId());
			history.insertHistory(history);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
			
			//微信同步
//			UserManager userApi = UserManagerImpl.create();
//			//经办人ID、积分卡编号、积分卡卡号
//			userApi.attnUpdateAccessCardInfo(attn.getAttnId(), attn.getAccessCardId(), attn.getAccessCardNo());
			
			MobileMessage message = new MobileMessage();
			message.putValue("attnId",attn.getAttnId());
			message.setMessage("TMS经办人积分信息修改功能调用[经办人/修改积分信息修改]接口失败,经办人ID为#attnId#");
			AttnUpdateAccessCardInfoBo api = new AttnUpdateAccessCardInfoBo();
			api.setMobileMessage(message);
			api.setAttnId(attn.getAttnId());
			api.setAccessCardId(attn.getAccessCardId());
			api.setAccessCardNo(attn.getAccessCardNo());
			PullingExecutor.getInstance().submit(api);
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
	
	/**
	 * view -> entity
	 * @param history
	 * @param view
	 * @return
	 */
	private IntegralcardUpdateHistory ToHistory(IntegralcardUpdateHistory history,AttnIntegralViewHelper view){
		if(StringUtil.isNullOrBlank(view.getAccessCardId())){
			history.setBusinessType(1);
			history.setAccessCardIdNew(view.getAccessCardIdNew());
			history.setAccessCardIdOld(view.getAccessCardIdNew());
			history.setAccessCardNoNew(view.getAccessCardNoNew());
			history.setAccessCardNoOld(view.getAccessCardNoNew());
		}else{
			history.setBusinessType(2);
			history.setAccessCardIdNew(view.getAccessCardIdNew());
			history.setAccessCardIdOld(view.getAccessCardId());
			history.setAccessCardNoNew(view.getAccessCardNoNew());
			history.setAccessCardNoOld(view.getAccessCardNo());
		}
		history.setMemberType(2);		
		
		if(StringUtil.isNullOrBlank(view.getChangePrice())){
			history.setChangePrice(0);
		}else{
			history.setChangePrice(Integer.parseInt(view.getChangePrice()));
		}
		
		history.setRemarks(view.getRemarks());
		
		return history;
	}

}
